<template>
  <j-modal :title="title" :visible="visible" @cancel="handleCancel" @ok="handleOk" width="30%" :okText="$t('保存')" :cancelText="$t('取消')" v-drag>
    <div>
      <a-row style="margin-left: 10px">
        <a-col :span="24">
          <a-form-model
            :rules="rules"
            :model="dataForm"
            ref="form"
            :label-col="labelCol"
            :wrapper-col="wrapperCol"
            v-enterToNext
          >
            <a-row>
              <a-col :span="24">
                <a-form-model-item :label="$t('税收模式')" prop="ivalue">
                  <j-dict-select-tag type="list" v-model="dataForm.ivalue" dictCode="tax_type"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="24" v-if="dataForm.ivalue == '1'">
                <a-form-model-item :label="$t('默认税率')" prop="svalue">
                  <a-input-number
                    v-model="dataForm.svalue"
                    style="width: calc(100% - 1em);"
                    :disabled="dataForm.ivalue == '0'"
                    :min="0"
                    :precision="4"
                  />%
                </a-form-model-item>
              </a-col>
              <a-col :offset="2" :span="20" v-if="dataForm.ivalue == '1'">
                <span style="color: #e38127;">{{$t('说明：默认各支付方式的税率均使用设定的默认税率；若个别支付方式税率不同，请根据实际需要进行调整。')}}</span>
              </a-col>
              <a-col :span="24" v-if="dataForm.ivalue == '2'">
                <a-form-model-item :label="$t('税率')" prop="svalue">
                  <a-input-number
                    v-model="dataForm.svalue"
                    style="width: calc(100% - 1em);"
                    :disabled="dataForm.ivalue == '0'"
                    :min="0"
                    :precision="4"
                  />%
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-form-model>
        </a-col>
      </a-row>
    </div>
  </j-modal>
</template>

<script>
import { httpAction,  getAction } from '@/api/manage'
export default {
  name: 'TaxSetModal',
  components: {},
  data() {
    return {
      model: {},
      title: this.$t('税收设置'),
      visible: false,
      dataForm: {
        name: 'taxSetting',
        ivalue: '0',
        svalue: 8.875,
        comment: this.$t('税收功能'),
      },
      labelCol: {
        xs: { span: 20 },
        sm: { span: 6 },
      },
      wrapperCol: {
        xs: { span: 20 },
        sm: { span: 16 },
      },
      rules: {
        ivalue: [{ required: true, message: this.$t('请选择') }],
        svalue: [{ required: true, message: this.$t('请输入') }],
      }
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  mounted() {},
  methods: {
    async show(){
      let that = this
      await that.getTax()
      that.visible = true
    },
    getTax() {
      let that = this
      return new Promise(resolve => {
        getAction('/bis/bisSysParameter/list', { name: 'taxSetting' }).then((res) => {
          if (res.success) {
            if (res.result.total > 0) {
              let obj = Object.assign({}, res.result.records[0])
              obj.ivalue = '' + obj.ivalue
              that.$set(that,'dataForm',obj)
              localStorage.setItem('taxSetting', that.dataForm.ivalue)
              localStorage.setItem('taxRate', that.dataForm.svalue)
            }
          } else {
            that.$message.error(res.message)
          }
          resolve()
        })
      })
    },
    handleOk() {
      let that = this
      that.$refs.form.validate((valid) => {
        if (valid) {
          let url = '', method = '';
          if (that.dataForm.id) {
            url = '/bis/bisSysParameter/edit'
            method = 'put'
          } else {
            url = '/bis/bisSysParameter/add'
            method = 'post'
          }
          httpAction(url, that.dataForm, method).then((res) => {
            if (res.success) {
              localStorage.setItem('taxSetting', that.dataForm.ivalue)
              localStorage.setItem('taxRate', that.dataForm.svalue)
              that.$message.success(res.message)
              that.close()
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          })
        }
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleCancel() {
      this.close()
    },
  },
}
</script>
<style scoped>
</style>